/**

 * 

 */
package com.newworld.dmsapp2.bean;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import org.litepal.crud.DataSupport;

import com.newworld.dmsapp2.util.Config;

/**
 * 
 * @author zuo
 * 
 * 
 */
@SuppressWarnings("serial")
public class User extends DataSupport implements Serializable {
	// ID

	private long id;
	// 用户ID
	private String userId;
	// 登录名

	private String username;
	
	// 用户类型


	private String usertype;// 1.户主、非户主 2.服务人员 3.派单员

	// 密码
	private String password;

	//
	private String sessionId;

	private String cookieName;

	// 返回显示信息
	private String message;

	// 返回成功、失败标志
	private String flag;

	// 返回任务个数
	private String total;
	// ip地址
	private String ip;

	// 功能
	private List<Function> functions = new ArrayList<Function>();

	// 用户信息

	private List<UserAccount> useraccounts = new ArrayList<UserAccount>();

	// 户内提单
	private RepairApply repairapply;
	// 公区提单
	private RepairApplyOutdoor repairapplayoutdoor;

	// 任务列表 进度、派单、改派、回访
	private List<Task> tasks = new ArrayList<Task>();

	// 维修人员
	private List<Server> servers = new ArrayList<Server>();

	public String getIp() {
		return ip;
	}

	public void setIp(String ip) {
		this.ip = ip;
	}

	public long getId() {
		return id;
	}

	public void setId(long id) {
		this.id = id;
	}

	public String getUserId() {
		return userId;
	}

	public void setUserId(String userId) {
		this.userId = userId;
	}

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public String getUsertype() {
		return usertype;
	}

	public void setUsertype(String usertype) {
		this.usertype = usertype;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public String getSessionId() {
		return sessionId;
	}

	public void setSessionId(String sessionId) {
		this.sessionId = sessionId;
	}

	public String getCookieName() {
		return cookieName;
	}

	public void setCookieName(String cookieName) {
		this.cookieName = cookieName;
	}

	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}

	public String getFlag() {
		return flag;
	}

	public void setFlag(String flag) {
		this.flag = flag;
	}

	public String getTotal() {
		return total;
	}

	public void setTotal(String total) {
		this.total = total;
	}

	public List<Function> getFunctions() {
		return functions;
	}

	public void setFunctions(List<Function> functions2) {
		this.functions = functions2;
	}

	public RepairApply getRepairapply() {
		return repairapply;
	}

	public void setRepairapply(RepairApply repairapply) {
		this.repairapply = repairapply;
	}

	public List<UserAccount> getUseraccounts() {
		return useraccounts;
	}

	public void setUseraccounts(List<UserAccount> useraccounts) {
		this.useraccounts = useraccounts;
	}

	public List<Task> getTasks() {
		return tasks;
	}

	public void setTasks(List<Task> tasks) {
		this.tasks = tasks;
	}

	public List<Server> getServers() {
		return servers;
	}

	public void setServers(List<Server> servers) {
		this.servers = servers;
	}

	public RepairApplyOutdoor getRepairapplayoutdoor() {
		return repairapplayoutdoor;
	}

	public void setRepairapplayoutdoor(RepairApplyOutdoor repairapplayoutdoor) {
		this.repairapplayoutdoor = repairapplayoutdoor;
	}

	// 根据functioncode获取functionurl
	public List<Function> getFunctionlistFromUser(String mfunctionCode) {
		Function rightfunction = null;
		List<Function> rightfunctions = new ArrayList<Function>();
		for (Function f : functions) {
			if (mfunctionCode.equals(f.getFunctionCode())) {
				rightfunction = DataSupport.find(Function.class, f.getId());
			}
		}
		rightfunctions.add(rightfunction);
		return rightfunctions;
	}

	// 根据userid获取任务列表
	public List<Task> getTasksFromUser() {
		return DataSupport.where("user_id = ?", String.valueOf(id)).find(
				Task.class);
	}

	//判断任务是否存在
		public boolean isTaskExistByRepairId(String repairId){
			boolean flag = false;
			List<Task> tasks = DataSupport.where("repairId = ? and user_id = ?",repairId,
					String.valueOf(id)).find(Task.class);
			if(null != tasks && !tasks.isEmpty() && tasks.size()>0){
				flag = true;
			}
			return flag;
			
		}
		//查询某条任务
		public List<Task> getTasksByRepairId(String repairId){
			List<Task> tasks = DataSupport.where("repairId = ? and user_id = ?",repairId,
					String.valueOf(id)).find(Task.class);
			return tasks;
			
		}
		
		//删除某条任务
		public boolean deleteTaskByRepairId(String repairId){
			boolean flag = false;
			DataSupport.deleteAll(Task.class,"repairId = ? and user_id = ?",repairId, String.valueOf(id));
			flag = true;
			return flag;
		}
		
		//删除某条任务
		public boolean deleteTaskByTaskId(long task_id){
			boolean flag = false;
			DataSupport.deleteAll(Task.class,"id = ? and user_id = ?",String.valueOf(task_id), String.valueOf(id));
			flag = true;
			return flag;
		}
				
	public List<Task> getFunctionTasksFromUser(String functionCode,
			String areaType) {
		List<Task> alltasks = DataSupport.where("user_id = ?",
				String.valueOf(id)).find(Task.class);
		List<Task> tasklist = new ArrayList<Task>();
		for (Task t : alltasks) {
			if (areaType.equals(Config.AREACODE_ALL)) {
				if (functionCode.equals(t.getFunctionCode())) {
					tasklist.add(t);
				}
			} else if (areaType.equals(Config.AREACODE_OUTDOOR)) {
				if (functionCode.equals(t.getFunctionCode())
						&& areaType.equals(t.getAreaType_typeCode())) {
					tasklist.add(t);
				}
			} else if (areaType.equals(Config.AREACODE_INDOOR_BUSINESS)) {
				if (functionCode.equals(t.getFunctionCode())
						&& (t.getAreaType_typeCode().equals(
								Config.AREACODE_INDOOR) || t
								.getAreaType_typeCode().equals(
										Config.AREACODE_BUSINESS))) {
					tasklist.add(t);
				}
			}

		}
		return tasklist;
	}

	// 根据插入数据库时间减序排列
	public List<Task> getTasksFromUserOrderByUpdateTime() {

		return DataSupport.where("user_id = ?", String.valueOf(id))
				.order("updatetime desc").find(Task.class);
	}

	// 删除所有任务
	public void deleteAllTask() {
		List<Task> tasks = DataSupport.where("user_id = ?", String.valueOf(id))
				.find(Task.class);
		for (Task task : tasks) {

			task.delete();
		}
	}

	// 删除所有functioncode
	public void deleteAllFunction() {
		List<Function> functions = DataSupport.where("user_id = ?",
				String.valueOf(id)).find(Function.class);
		for (Function function : functions) {

			function.delete();
		}
	}

}
